package com.cjack.frenchexcel.service;

import com.cjack.frenchexcel.dao.SystemparamDao;
import com.cjack.frenchexcel.exception.CommonException;
import com.cjack.frenchexcel.model.SystemparamModel;
import com.cjack.frenchexcel.table.SystemparamTable;
import com.cjack.frenchexcel.util.ModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * Created by root on 10/5/19
 */
@Service
public class SystemparamService {

    @Autowired
    private SystemparamDao dao;

    public List<SystemparamModel> list() {
        Sort sort = new Sort( Sort.Direction.DESC, "createTime");
        return ModelUtils.copyListModel( dao.findAll( sort), SystemparamModel.class);
    }

    public void add( SystemparamModel model) {
        SystemparamTable table;
        if( model.getId() == null){
            table = ModelUtils.copySignModel( model, SystemparamTable.class);
        }else{
            table = dao.findOne( model.getId());
            ModelUtils.copySignModel( model, table);
        }

        dao.save( table);
    }

    public SystemparamModel getParam(){
        return ModelUtils.copySignModel( dao.getFirstByIdNotNull(), SystemparamModel.class);
    }
}
